/********************************************************************************
 *@description: 库存预警
 *@author: Xhd
 *@API: 卢彦铮
 *@date: 2024-03-21 11:07:33
 *@version: V1.0.0
**********************************************************************************/

import React, { Component } from 'react';
import TheQueryForm from '@/components/TheQueryForm';
import TheTable from '@/components/TheTable';
import ThePagination from '@/components/ThePagination';
import TheButton from '@/components/TheButton';
import store from "@/store/store";
import _ from 'lodash';
import { getDateTime3 } from '@/utils/getDate';
import { apiStockWarnPage, apiStockWarnDel } from '@/request/storageManager/inventoryAlerts';

class InventoryAlerts extends Component {
  constructor(props) {
    super(props)
    this.queryFormRef = React.createRef();
    this.state = {
      columns: [
        {
          title: "商品ID",
          dataIndex: "goodsId",
          key: "goodsId",
        },
        {
          title: "商品名称",
          dataIndex: "goodsName",
          key: "goodsName",
        },
        {
          title: "商品规格名称",
          dataIndex: "goodsSkuName",
          key: "goodsSkuName",
        },
        {
          title: "仓库ID",
          dataIndex: "libraryId",
          key: "libraryId",
        },
        {
          title: "仓库名称",
          dataIndex: "libraryName",
          key: "libraryName",
        },
        {
          title: "商品物料名称",
          dataIndex: "materialName",
          key: "materialName",
        },
        {
          title: "物料编码",
          dataIndex: "materialId",
          key: "materialId",
        },
        {
          title: "预警数量",
          dataIndex: "sysWarnCount",
          key: "sysWarnCount",
        },
        {
          title: "预警时库存数量",
          dataIndex: "warnStock",
          key: "warnStock",
        },
        {
          title: "当前库存数量",
          dataIndex: "currentStock",
          key: "currentStock",
        },
        {
          title: "预警时间",
          dataIndex: "createTime",
          key: "createTime",
        },
        {
          title: "操作",
          dataIndex: "serviceShowFile",
          key: "serviceShowFile",
          fixed: 'right',
          render: (text, render) => {
            return <div>
              <TheButton haspermi={['storage:inventory:delete']} onClick={() => this.delete(render.id)}>删除</TheButton>
            </div>
          }
        },
      ],
      dataSource: [],
      pageData: { pageNo: 1, pageSize: 10 },
      total: 0,
      queryForm: {},
      searchOption: [
        { field: 'goodsId', label: '商品ID', placeholder: '请输入商品ID', required: false, rules: [], type: 'Input', defaultValue: '' },
        { field: 'goodsName', label: '商品名称', placeholder: '请输入商品名称', required: false, rules: [], type: 'Input', defaultValue: '' },
        { field: 'libraryId', label: '仓库ID', placeholder: '请输入仓库ID', required: false, rules: [], type: 'Input', defaultValue: '' },
        { field: 'libraryName', label: '仓库名称', placeholder: '请输入仓库名称', required: false, rules: [], type: 'Input', defaultValue: '' },
        { field: 'materialId', label: '物料ID', placeholder: '请输入物料ID', required: false, rules: [], type: 'Input', defaultValue: '' },
        { field: 'times', label: '预警时间', placeholder: '', required: false, rules: [], type: 'RangePicker', defaultValue: '' },
      ],
    }
  }

  componentDidMount() {
    let searchParams = store.getState().searchValue
    let { pageNo, pageSize, queryForm, pathName } = searchParams
    if (searchParams && pageNo && pathName == 'inventoryAlerts') {
      this.setState({
        queryForm,
        pageData: {
          pageNo: pageNo, pageSize: pageSize
        }
      }, () => {
        this.queryFormRef.onSetDefault({
          ...queryForm
        })
        this.getData()
      })
    } else {
      this.getData()
    }
  }

  // 获取数据
  getData = async (values) => {
    let { pageNo, pageSize } = this.state.pageData
    if (values && !_.isEmpty(values.times)) {
      values.startTime = getDateTime3(values.times[0]) + ' 00:00:00'
      values.endTime = getDateTime3(values.times[1]) + ' 23:59:59'
      delete (values.times)
    }
    this.setState({
      queryForm: values === undefined ? this.state.queryForm : { ...values }
    }, async () => {
      const params = {
        pageNo, pageSize, queryForm: this.state.queryForm
      }
      store.dispatch({
        type: 'SaveSearchValues',
        data: {
          pathName: 'inventoryAlerts',
          ...params
        }
      })
      let res = await apiStockWarnPage({
        pageNo, pageSize, ...this.state.queryForm
      })
      if (res.code === 0) {
        this.setState({
          dataSource: res.data,
          total: res.totalCount
        })
      }
    })
  }

  // 翻页
  pageChange = (pageNo, pageSize) => {
    this.setState({
      pageData: {
        pageNo,
        pageSize
      }
    }, () => this.getData())
  }

  // 重置
  onReset() {
    this.setState({
      queryForm: {}
    }, () => {
      this.pageChange(1, 10)
    })
  }

  // 搜索
  onFormSearch(values) {
    this.setState({
      pageData: { pageNo: 1, pageSize: this.state.pageData.pageSize }
    }, () => {
      this.getData(values)
    })
  }

  // 删除
  delete = async (id) => {
    React.$confirm({
      title: "删除确认",
      content: "确认删除该商品库存预警吗？",
      onOk: async () => {
        let res = await apiStockWarnDel({ id })
        if (res.code === 0) {
          React.$message({
            type: "success",
            content: "删除成功"
          })
          this.getData()
        }
      }
    })
  }

  render() {
    const { columns, dataSource, pageData: { pageNo, pageSize }, total, searchOption, } = this.state;
    return (<div className='p-layout-box'>
      <TheQueryForm ref={(ref) => (this.queryFormRef = ref)} show={true} option={searchOption} onReset={() => this.onReset()} onGetValues={(values) => this.onFormSearch(values)} />
      <TheTable scroll={{ x: 'max-content', y: '63vh' }} columns={columns} dataSource={dataSource} />
      <ThePagination pageNo={pageNo} pageSize={pageSize} total={total} pageChange={this.pageChange} />
    </div>)
  }
}

export default InventoryAlerts
